import React from 'react';
import { Button } from 'antd';

/**
 * 用法同antd button 可传len设置按钮字数 来设置宽度
 * @param props
 * @returns {*}
 */
export default props => {
  const { len, children, ...others } = props;
  let text = children;
  // 填充空格
  if (len && typeof text === 'string') {
    // eslint-disable-next-line no-control-regex
    const { length } = children.replace(/[^\u0000-\u00ff]/g, 'aaa');
    const space = 3 * len - length;
    const frontLen = Math.floor(space / 2);
    const backLen = space - frontLen;

    if (backLen > 0) {
      text =
        new Array(frontLen).fill('\u00a0').join('') +
        text +
        new Array(backLen).fill('\u00a0').join('');
    }
  }
  return (
    <Button type="primary" {...others}>
      {text}
    </Button>
  );
};
